package com.zone.trigger.http;

import com.zone.domain.award.model.entity.DistributeAwardEntity;
import com.zone.domain.award.model.entity.UserAwardRecordEntity;
import com.zone.domain.award.service.IAwardService;
import com.zone.trigger.api.IRaffleAwardService;
import com.zone.trigger.api.dto.AwardDistributeDTO;
import com.zone.types.model.Response;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2025/3/10
 */
@CrossOrigin("${app.config.cross-origin}")
@RestController
@RequestMapping("/api/${app.config.api-version}/raffle/award")
@DubboService(version = "1.0")
public class RaffleAwardController implements IRaffleAwardService {
    @Autowired
    private IAwardService awardService;

    @PostMapping("/distribute")
    @Override
    public Response<Boolean> distribute(@RequestBody AwardDistributeDTO request) {
        // 查询用户中奖订单
        UserAwardRecordEntity userAwardRecordEntity = awardService.queryUserAwardRecord(request.getUserId(), request.getOrderId());

        // 发奖
        DistributeAwardEntity distributeAwardEntity = new DistributeAwardEntity();
        distributeAwardEntity.setUserId(request.getUserId());
        distributeAwardEntity.setActivityId(userAwardRecordEntity.getActivityId());
        distributeAwardEntity.setAwardId(userAwardRecordEntity.getAwardId());
        distributeAwardEntity.setAwardConfig(userAwardRecordEntity.getAwardConfig());
        distributeAwardEntity.setOrderId(request.getOrderId());
        awardService.distribute(distributeAwardEntity);
        return Response.success(true);
    }
}
